package com.stardata.liangshisq.tcslmon.controller;

import com.stardata.liangshisq.tcslmon.annotation.IgnoreAuth;
import com.stardata.liangshisq.tcslmon.util.ApiBaseAction;
import com.stardata.liangshisq.tcslmon.util.R;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.apache.log4j.Logger;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Samson Shu
 * @email shush@stardata.top
 * @date 2020/8/6 23:23
 */
@Api(tags = "websocket消息测试接口")
@RestController
@AllArgsConstructor
public class KitchenMsgController extends ApiBaseAction {
  private final Logger logger = Logger.getLogger(getClass());
  private final SimpMessagingTemplate messagingTemplate;

  @MessageMapping("/v1/hello")
  @IgnoreAuth
  public String greeting(String message) {
    logger.info(
        String.format("<==== Received message [%s] ", message));
    return message;
  }

  @GetMapping("/v1/test_websocket")
  @IgnoreAuth
  public R notifyWebsocket(String topic, String message) {
    if (message == null) message = "";
    messagingTemplate.convertAndSend(topic, message);
    return success();
  }
}
